//
//  WEXWelcomeViewController.swift
//  microBlog
//
//  Created by apple on 16/4/20.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit
import SDWebImage

class WEXWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        setupUI()
    }
    
    private func setupUI(){
        
        view.backgroundColor = UIColor(white: 247/255.0, alpha: 1)
        
        //添加控件
        view.addSubview(userIcon)
        view.addSubview(messageLabel)
        
        //添加约束
        userIcon.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 80, height: 80))
            make.centerX.equalTo(view)
            make.top.equalTo(200)
            
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(userIcon.snp_bottom).offset(15)
            make.centerX.equalTo(userIcon.snp_centerX)
        }
        
    }
    
    
    // MARK: - 在界面加载完成的时候执行动画
    override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated)
        
        userIcon.snp_updateConstraints { (make) -> Void in
            
            make.top.equalTo(100)
        }
        
        
        UIView .animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            //刷新界面
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(WEXChangeRootVCNotification, object: self)
                        
                })
        }
    }
    
    
    
    // MARK: - 懒加载控件
    // 用户头像
    private lazy var userIcon: UIImageView = {
        
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        icon.layer.cornerRadius = 40
        icon.layer.masksToBounds = true
        
        let viewModel = WEXUserAccountViewModel.sharedViewModel
        
        
//        取到头像地址，去加载头像
        if let view = viewModel.account?.avatar_large  {
            
            icon.sd_setImageWithURL(NSURL(string: view), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        
        
        return icon
    }()
    
    
    // 文字
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, preferredMaxLayoutWidth: 240)
        
        label.text = "欢迎回来"
        
        label.textAlignment = .Center
        
        label.alpha = 0
        
        return label
    }()
  
}
